package com.huaxi.ddgw.manager.task;

import cn.hutool.core.date.DateUtil;
import com.huaxi.ddgw.manager.mapper.OrderInfoMapper;
import com.huaxi.ddgw.manager.mapper.OrderStatisticsMapper;
import com.huaxi.ddgw.model.entity.order.OrderStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class OrderStatisticsTask {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    //每天凌晨两点执行 把统计数据添加到结果表
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics() {
        //获取前一天的日期
        String createDate = DateUtil.offsetDay(new Date(), -1).toString(new SimpleDateFormat("yyyy-MM-dd"));
        //根据前一天的日期进行统计功能 (分组操作)
        //统计前一天交易的数据
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createDate);
        //把统计的数据添加到结果表
        if (orderStatistics != null) {
            orderStatisticsMapper.insert(orderStatistics);
        }
    }
}
